package com.edu.zju.scs.panacea.controller.plan;

import com.edu.zju.scs.panacea.entity.BaseResult;
import com.edu.zju.scs.panacea.entity.plan.Seller;
import com.edu.zju.scs.panacea.entity.resource.Site;
import com.edu.zju.scs.panacea.service.plan.SellerService;
import com.edu.zju.scs.panacea.service.resource.SiteService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @Author: Yue Liang
 * @Date: 2018-12-12 18:53
 */

@Controller
@EnableAutoConfiguration
public class SellerContoller {
    @Autowired
    private SellerService sellerService;

    @ResponseBody
    @ApiOperation(value="所有商家查询", notes="所有商家查询")
    @RequestMapping(value = "/seller/all", method = RequestMethod.GET)
    public BaseResult getAllSite() {
        return sellerService.getAllSeller();
    }

    @ResponseBody
    @ApiOperation(value="根据id查询某个商家", notes="根据id查询某个商家")
    @RequestMapping(value = "/seller/{id}", method = RequestMethod.GET)
    public BaseResult getSeller(@PathVariable Long id) {
        return sellerService.getSeller(id);
    }

    @ResponseBody
    @ApiOperation(value="根据ID删除商家", notes="根据ID删除商家")
    @RequestMapping(value = "/seller/deletion/{id}", method = RequestMethod.GET)
    public BaseResult deleteSeller(@PathVariable Long id) {
        return sellerService.deleteSeller(id);
    }

    @ResponseBody
    @ApiOperation(value="新增商家", notes="新增商家")
    @RequestMapping(value = "/seller/new", method = RequestMethod.POST)
    public BaseResult addSeller(
            @ApiParam("商家自有ID") @RequestParam(value = "ownId") String ownId,
            @ApiParam("商家名称") @RequestParam(value = "name") String name) {
        Seller seller = new Seller(ownId, name);
        return sellerService.addSeller(seller);
    }

    @ResponseBody
    @ApiOperation(value="修改商家信息", notes="修改商家信息")
    @RequestMapping(value = "/seller/edit", method = RequestMethod.POST)
    public BaseResult editSeller(
            @ApiParam("ID") @RequestParam(value = "id") Long id,
            @ApiParam("自有ID") @RequestParam(value = "ownId") String ownId,
            @ApiParam("商家名称") @RequestParam(value = "name") String name) {
        Seller seller = new Seller(id, ownId, name);
        return sellerService.editSeller(seller);
    }

    @ResponseBody
    @ApiOperation(value="商家登录", notes="商家登录")
    @RequestMapping(value = "/seller/login", method = RequestMethod.GET)
    public BaseResult sellerLogin(
            @ApiParam("用户名") @RequestParam(value = "name") String name,
            @ApiParam("密码") @RequestParam(value = "password") String password,
            HttpSession session) {
        return sellerService.isValidSeller(name, password, session);
    }
}
